package fr.utbm.gi.in56.vehicle;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;


import fr.utbm.gi.in56.database.DatabaseManager;

/**
 * Implementation of a vehicle manager
 *
 * @author Adrien Hoffer & Tarik Belabbas
 *
 */
public class VehicleManager extends DatabaseManager
{

	public VehicleManager( Connection _connection ) throws SQLException
	{
		super( _connection );
	}
	
	/**
     * Returns a map of the energies stored in the database
     * 
     * @return the map of the energies stored in the database
     */
	public HashMap< Integer, String > getEnergiesList()
	{
		HashMap< Integer, String > energiesMap = new HashMap< Integer, String >();
		
		ResultSet el = null;
		
		try
		{
			Statement statement = this.getNewStatement();
			el = statement.executeQuery( "SELECT * FROM energy" ); //$NON-NLS-1$
			if ( el.first() )
			{
				energiesMap.put(
						el.getInt( "id_energy" ), el.getString( "energy" ) ); //$NON-NLS-1$ //$NON-NLS-2$
				while ( el.next() )
				{
					energiesMap.put(
							el.getInt( "id_energy" ), el.getString( "energy" ) ); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}

		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}
		try
		{
			el.close();
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}
		
		return energiesMap;
	}
    
    /**
     * Returns a map of the car brands stored in the database
     * The key is the id of the brand and the value is the name
     * 
     * @return  a map of the car brands stored in the database
     */
    public HashMap< Integer, String > getCarBrands()
    {
    	HashMap< Integer, String > carBrandsMap = new HashMap< Integer, String >();
    	ResultSet rst = null;
    	
    	try
    	{
    		rst = this.getNewStatement().executeQuery( "SELECT * FROM car_brands" ); //$NON-NLS-1$
    		
			if ( rst.first() )
			{
				do
				{
					carBrandsMap
							.put( rst.getInt( "id_car_brands" ), rst.getString( "brand" ) ); //$NON-NLS-1$ //$NON-NLS-2$
				}
				while ( rst.next() );
			}
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}
		
		try
		{
			if(rst != null)
			{
				rst.close();
			}
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}
		
    	return carBrandsMap;
    }
    
    /**
     * Returns a map containing all the brands and models stored in the database.
     * The key contains a {@link BrandEntry} and the value contains some {@link ModelEntry}
     * 
     * @param _vehicleType the type of vehicle
     * @return a map containing all the brands and models
     */
	public HashMap< BrandEntry, LinkedList< ModelEntry > > getBrandsAndModels()
	{
		HashMap< BrandEntry, LinkedList< ModelEntry > > modelsAndBrandMap = 
			new HashMap< BrandEntry, LinkedList< ModelEntry > >();

		ResultSet rstBrands = null;
		ResultSet rstModels = null;

		try
		{
			rstBrands = this.getNewStatement().executeQuery(
					"SELECT * FROM car_brands" ); //$NON-NLS-1$ //$NON-NLS-2$
			if ( rstBrands.first() )
			{
				Statement statement = this.getNewStatement();
				do
				{
					BrandEntry brand = new BrandEntry(
							rstBrands.getInt( "id_car_brands" ), //$NON-NLS-1$ //$NON-NLS-2$
							rstBrands.getString( "brand" ) ); //$NON-NLS-1$
					rstModels = statement.executeQuery(
									"SELECT * FROM car_models WHERE id_brand = '" //$NON-NLS-1$
							+ brand.getId() + "'" ); //$NON-NLS-1$ //$NON-NLS-2$
					LinkedList< ModelEntry > models = new LinkedList< ModelEntry >();
					if ( rstModels.first() )
					{
						do
						{
							ModelEntry model = new ModelEntry(
									rstModels.getInt( "id_car_models" ), //$NON-NLS-1$
									rstModels.getString( "model" ) ); //$NON-NLS-1$
							models.add( model );
						}
						while ( rstModels.next() );
					}
					
					modelsAndBrandMap.put( brand, models );
				}
				while ( rstBrands.next() );
				
				rstBrands.close();
				rstModels.close();
			}
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}
		return modelsAndBrandMap;
	}

}